<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Site Search Class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	File Storage
 * @version		1.0
 * @author		Matthew Glinski
 * @link		---- Not Yet Documented or Hosted ----
 */
require_once('Zend/Search/Lucene.php');

class Lucenesearch
{
	public $indexPath = '/var/www/vhosts/thinkingcity.com/httpdocs/search_fda34e/index';
	public $index = '';
	public $newIndex = false;
		
	function Lucenesearch()
	{
		if($_SERVER['HTTP_HOST'] == 'localhost')
		{
			$this->indexPath = 'search_fda34e/index';
		}
		$this->openIndex();
	}
	
	function openIndex()
	{
		try
		{
			$this->index = Zend_Search_Lucene::open($this->indexPath);
		}
		catch(Zend_Search_Lucene_Exception $ex)
		{
			die($ex->getMessage());
		}
	}
	
	function query($q, $field='')
	{		
		if($field)
			$q .= " page:".$field;
			
		try 
		{
        	$results = $this->index->find($q);
		}
		catch (Zend_Search_Lucene_Exception $ex) 
		{
			$results = $ex->getMessage();
		}
		
		return $results;
	}
	
	function newIndex()
	{		
		try
		{
			$this->index = Zend_Search_Lucene::create($this->indexPath);
		}
		catch(Zend_Search_Lucene_Exception $ex)
		{
			print_r($ex);
			die();
		}
		
		$this->newIndex = true;
	}
	
	function updateItem($oldID, $doc, $url, $page)
	{
		// find the document based on the indexed rand_id field
		$term = new Zend_Search_Lucene_Index_Term($oldID, 'rand_id');
		foreach ($index->termDocs($term) as $id)
			$index->delete($id);
	 
		// re-add the document
		$this->addItem($doc, $url, $page);

		$this->rewriteIndex();
	}
	
	function deleteItem($rid)
	{
		// find the document based on the indexed rand_id field
		$term = new Zend_Search_Lucene_Index_Term($rid, 'rand_id');
		foreach ($this->index->termDocs($term) as $id)
		{
			$this->index->delete($id);
		}
	 
		$this->rewriteIndex();
	}
	
	function addItem($doc, $url, $page)
	{		
		$this->index->addDocument(new CISearchIndexedDocument($doc, $url, $page));
		$this->rewriteIndex();
	}
	
	function rewriteIndex()
	{
		$this->index->commit();
	}
}

#######################################
#######################################
#######################################
#######################################
#######################################


class CISearchIndexedDocument extends Zend_Search_Lucene_Document
{
	/**
	 * Constructor. Creates our indexable document and adds all
	 * necessary fields to it using the passed in document
	 */
	public function __construct($document, $url, $page)
	{
		$this->addField(Zend_Search_Lucene_Field::Keyword('rand_id', $document->rand_id));
		$this->addField(Zend_Search_Lucene_Field::UnIndexed('url',       $url));
		$this->addField(Zend_Search_Lucene_Field::UnIndexed('created',   $document->date));
		$this->addField(Zend_Search_Lucene_Field::UnIndexed('preview',   substr($document->body, 0, 250)));
		$this->addField(Zend_Search_Lucene_Field::Text('title',          $document->title));
		$this->addField(Zend_Search_Lucene_Field::Text('tags',          $document->tags));
		$this->addField(Zend_Search_Lucene_Field::Text('author',         $document->author));
		$this->addField(Zend_Search_Lucene_Field::Text('page',         $page));
		$this->addField(Zend_Search_Lucene_Field::UnStored('content',    $document->body));
	}
}
?>